﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE;
using EnvDTE80;

namespace SinacorRules
{
    public sealed class Rule_AttributesMethodsCheck : BaseAddIn.Rules.BaseRuleAddIn
    {
        public Rule_AttributesMethodsCheck() :
            base("AttributesMethodsCheck", "SinacorRules.SinacorRules", typeof(Rule_AttributesMethodsCheck).Assembly)
        {
        }

        protected sealed override void Check(CodeInterface2 inter)
        {
            CodeAttribute atributoInter;
            CodeElement2 aux;
            CodeFunction function;
            CodeAttribute atributoFunction;
            bool analisaInterface = false;
            bool hasGeneralFault = false;
            bool hasBusinessFault = false;
            bool hasValidationFault = false;
            bool hasInvalidSessionFault = false;
            bool hasLicensingFault = false;
            bool hasAccessIsDeniedFault = false;
            bool breakLoop = false;

            for (int countInterAttributes = 1; countInterAttributes <= inter.Attributes.Count; countInterAttributes++)
            {
                atributoInter = (CodeAttribute)inter.Attributes.Item(countInterAttributes);

                if (atributoInter.Name == "ServiceContract")
                {
                    analisaInterface = true;
                    break;
                }
            }

            if (analisaInterface)
            {
                for (int countInterChildren = 1; countInterChildren <= inter.Children.Count; countInterChildren++)
                {
                    aux = (CodeElement2)inter.Children.Item(countInterChildren);

                    if (aux.Kind == vsCMElement.vsCMElementFunction)
                    {
                        function = (CodeFunction)aux;

                        for (int countFunctionAttributes = 1; countFunctionAttributes <= function.Attributes.Count; countFunctionAttributes++)
                        {
                            atributoFunction = (CodeAttribute)function.Attributes.Item(countFunctionAttributes);

                            if (atributoFunction.Value.Contains("GeneralFault"))
                                hasGeneralFault = true;
                            else if (atributoFunction.Value.Contains("BusinessFault"))
                                hasBusinessFault = true;
                            else if (atributoFunction.Value.Contains("ValidationFault"))
                                hasValidationFault = true;
                            else if (atributoFunction.Value.Contains("InvalidSessionFault"))
                                hasInvalidSessionFault = true;
                            else if (atributoFunction.Value.Contains("LicensingFault"))
                                hasLicensingFault = true;
                            else if (atributoFunction.Value.Contains("AccessIsDeniedFault"))
                                hasAccessIsDeniedFault = true;
                        }

                        if (!hasGeneralFault)
                        {
                            GeraProblemaAddIn(function.StartPoint.Parent.Parent.FullName,
                                             function.StartPoint.Line,
                                             function.StartPoint.DisplayColumn,
                                             "GeneralFaultAttributeResolution",
                                             inter.Name,
                                             function.Name,
                                             inter.Namespace.Name);

                            breakLoop = true;
                        }

                        if (!hasBusinessFault)
                        {
                            GeraProblemaAddIn(function.StartPoint.Parent.Parent.FullName,
                                              function.StartPoint.Line,
                                              function.StartPoint.DisplayColumn,
                                              "BusinessFaultAttributeResolution",
                                              inter.Name,
                                              function.Name,
                                              inter.Namespace.Name);

                            breakLoop = true;
                        }

                        if (!hasValidationFault)
                        {
                            GeraProblemaAddIn(function.StartPoint.Parent.Parent.FullName,
                                             function.StartPoint.Line,
                                             function.StartPoint.DisplayColumn,
                                             "ValidationFaultAttributeResolution",
                                             inter.Name,
                                             function.Name,
                                             inter.Namespace.Name);

                            breakLoop = true;
                        }

                        if (!hasInvalidSessionFault)
                        {
                            GeraProblemaAddIn(function.StartPoint.Parent.Parent.FullName,
                                             function.StartPoint.Line,
                                             function.StartPoint.DisplayColumn,
                                             "InvalidSessionFaultAttributeResolution",
                                             inter.Name,
                                             function.Name,
                                             inter.Namespace.Name);

                            breakLoop = true;
                        }

                        if (!hasLicensingFault)
                        {
                            GeraProblemaAddIn(function.StartPoint.Parent.Parent.FullName,
                                             function.StartPoint.Line,
                                             function.StartPoint.DisplayColumn,
                                             "LicensingFaultAttributeResolution",
                                             inter.Name,
                                             function.Name,
                                             inter.Namespace.Name);

                            breakLoop = true;
                        }

                        if (!hasAccessIsDeniedFault)
                        {
                            GeraProblemaAddIn(function.StartPoint.Parent.Parent.FullName,
                                             function.StartPoint.Line,
                                             function.StartPoint.DisplayColumn,
                                             "AccessIsDeniedFaultAttributeResolution",
                                             inter.Name,
                                             function.Name,
                                             inter.Namespace.Name);

                            breakLoop = true;
                        }

                        if (breakLoop)
                            break;
                    }
                }
            }
        }
    }
}
